package com.servicesystem.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mike.module.core.entity.ElementEntity;
import com.mike.module.core.expect.ApiServiceException;
import com.mike.module.core.expect.TagException;
import com.mike.module.core.region.FlowRegion;
import com.mike.module.core.utils.ErrorEnumUtil;
import com.mike.module.core.utils.SpringContextUtil;
import com.mike.module.core.work.FlowWork;
import com.servicesystem.demo.entity.ServiceData;
import com.servicesystem.demo.utils.ErrorResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemService
 * @Description TODO
 * @Author Mike
 * @Date 2019/6/18 14:04
 * @Version 1.0
 */
@Service
public class SystemService {

    @Value("mike.parse.xml.sql.getServiceName")
    private String sqlGetServiceName;

    public JSONObject entrance(ServiceData serviceData){
        //校验 apiName
        if(!serviceData.checkApiName()){return ErrorResponse.ERR_JSON.getResponse();}
        //校验服务名是否存在
        if(!serviceData.checkServiceName()){return ErrorResponse.NO_SERVICE.getResponse();}
        //参数鉴权
        if(!serviceData.checkAuthority()){return ErrorResponse.ERR_TOKEN.getResponse();}
        //用户是否有权使用此服务
        if(!serviceData.checkServiceAuthority()){return ErrorResponse.NO_AUTHORITY.getResponse();}
        //获取服务数据
        List<ElementEntity> list = serviceData.getWorkList();
//        System.out.println(list);
        if(list == null){
            return ErrorResponse.NO_XMLLIST.getResponse();
        }
        //获取 服务参数和业务参数
        Map<String,Object> hm = serviceData.getHeadMap();
        Map<String,Object> bm = serviceData.getBodyMap();
        //获取服务工作流对象
        FlowRegion region = SpringContextUtil.getBean("flowRegion");
        FlowWork work = SpringContextUtil.getBean("flowWork");
        //
        String apiName = serviceData.getHead().getApiName();
        try{
            region.init(apiName,list,bm,hm);
        }catch(Exception e){
            return ErrorResponse.FAIL_REGION_INIT.getResponse();
        }
        try{
            work.init(region);
        }catch (Exception e){
            return ErrorResponse.FAIL_WORK_INIT.getResponse();
        }
        try{
            work.workStart();
        }catch (ApiServiceException e){
            region.addErrorMsg(e.toMap());
            region.setError(e.toMap());
        }catch(Exception e){
            region.addErrorMsg(new ApiServiceException(apiName,"error!",ErrorEnumUtil.ERR_UNKNOW).toMap());
            region.setError(new ApiServiceException(apiName,"error!",ErrorEnumUtil.ERR_UNKNOW).toMap());
        }
        Map<String,Object> rm = null;
        if(region.isError()){
            rm = new HashMap<>();
            Map<String,Object> headMap = serviceData.getHeadMap();
            if(region.isHaveExcept()){
                headMap.put("Exception",region.getExpectList());
            }
            rm.put("Head",headMap);
        }else{
            rm = region.getResponse(apiName);
            if(rm ==null){
                rm = new HashMap<String,Object>();
                rm.put("Head",serviceData.getHeadMap());
            }
            if(rm.containsKey("Head") && region.isHaveExcept()){
                Map<String,Object> headMap = (Map<String, Object>) rm.get("Head");
                headMap.put("Exception",region.getExpectList());
                rm.put("Head",headMap);
            }
        }
        return JSONObject.parseObject(JSON.toJSONString(rm));
    }

}
